package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.LoginAnnotation;
import cn.wolfcode.wolf2w.annotation.UserInfoResolver;
import cn.wolfcode.wolf2w.domain.*;
import cn.wolfcode.wolf2w.domain.vo.TravelStatisVO;
import cn.wolfcode.wolf2w.query.TravelCommentQuery;
import cn.wolfcode.wolf2w.query.TravelQuery;
import cn.wolfcode.wolf2w.redis.IRedisTravelStatisService;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.ITravelCommentService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.util.JsonResult;
import cn.wolfcode.wolf2w.util.UMEditorUploader;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;


@RestController
@RequestMapping("travels")
public class TravelController {
    @Autowired
    private ITravelService travelService;
    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private ITravelCommentService travelCommentService;
    @Autowired
    private IRedisTravelStatisService travelStatisService;

     @GetMapping("query")
    public Object list(TravelQuery qo){
        Page<Travel> list = travelService.queryPageList(qo);
        return JsonResult.success(list);
    }
    @LoginAnnotation
    @PostMapping("saveOrUpdate")
    public Object saveOrUpdate(Travel travel,@UserInfoResolver UserInfo userInfo){
         travel.setUserId(userInfo.getId());
        travelService.saveOrUpdate(travel);
        return JsonResult.success();
    }

    /**
     * 游记
     * @param id
     * @return
     */
    @GetMapping("detail")
    public Object detail(String id){
        //阅读数增加
        travelStatisService.addViewnum(id);

        Travel travel = travelService.get(id);
        return JsonResult.success(travel);
    }


    @GetMapping("input")
    public Object input(String id){
        HashMap<Object, Object> map = new HashMap<>();
        if(StringUtils.hasLength(id)){
            List<Destination> dests = destinationService.list();
            Travel travel = travelService.get(id);
            map.put("dests",dests);
            map.put("tv",travel);
        }else{
            List<Destination> dests = destinationService.list();
            map.put("dests",dests);
        }
        return JsonResult.success(map);
    }

    //上传图片
    @PostMapping("/contentImage")
    public String uploadUEImage(MultipartFile upfile, HttpServletRequest request) throws Exception{
        UMEditorUploader up = new UMEditorUploader(request);
        String[] fileType = {".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile);

        String callback = request.getParameter("callback");
        String result = "{\"name\":\""+ up.getFileName() +"\", \"originalName\": \""+ up.getOriginalName() +"\", \"size\": "+ up.getSize()
                +", \"state\": \""+ up.getState() +"\", \"type\": \""+ up.getType() +"\", \"url\": \""+ up.getUrl() +"\"}";
        result = result.replaceAll( "\\\\", "\\\\" );
        if(callback == null ){
            return result ;
        }else{
            return "<script>"+ callback +"(" + result + ")</script>";
        }
    }

    /**
     * 游记评论添加
     * @param travelComment
     * @param userInfo
     * @return
     */
    @LoginAnnotation
    @PostMapping("commentAdd")
    public Object addComment(TravelComment travelComment, @UserInfoResolver UserInfo userInfo){
        BeanUtils.copyProperties(userInfo,travelComment);
        travelComment.setUserId(userInfo.getId());
        travelCommentService.save(travelComment);
        return JsonResult.success();
    }

    /**
     * 游记评论列表
     * @param qo
     * @return
     */
    @GetMapping("comments")
    public Object comments(@ModelAttribute("qo") TravelCommentQuery qo){
        List<TravelComment> list = travelCommentService.comments(qo);
        return JsonResult.success(list);
    }

    @GetMapping("statisVo")
    public Object statisVo(String tid){
        //数据回显
        TravelStatisVO vo = travelStatisService.getStatisVo(tid);
        return JsonResult.success(vo);
    }


    /**
     * 点赞操作
     * @param tid
     * @return true: 点赞成功  false:点赞失败
     */
    @LoginAnnotation
    @PostMapping("travelThumbup")
    public Object travelThumbup(String tid,@UserInfoResolver UserInfo userInfo){
        Boolean ret = travelStatisService.travelThumbup(tid,userInfo.getId());
        return JsonResult.success(ret);
    }

}